package com.jyou.stock;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;

public class HttpUtil {
	private String url = null;
	private int timeoutInMS = 50000;
	
	public HttpUtil(String url){
		this.url = url;
	}
	
	private ByteArrayOutputStream getResponse(String url, boolean isPost, int timeoutInMS)
			throws Exception {

		String cookie = null;
		HttpURLConnection conn = null;
		InputStream inputStream = null;
		DataOutputStream out = null;
		
		try {
			URL uUrl = null;			

			if (isPost) {
				uUrl = new URL(url);
				conn = (HttpURLConnection) uUrl.openConnection();
				conn.setRequestMethod("POST");
			} else {
				uUrl = new URL(url);
				conn = (HttpURLConnection) uUrl.openConnection();
				conn.setRequestMethod("GET");
			}
			conn.setRequestProperty("Proxy-Connection", "Keep-Alive");

			conn.setReadTimeout(timeoutInMS);
			conn.setConnectTimeout(timeoutInMS);
			conn.setDoOutput(true);
			conn.setDoInput(true);

			if (isPost) {
				out = new DataOutputStream(conn.getOutputStream());
				out.flush();
			}

			inputStream = conn.getInputStream();

			byte[] bytes = new byte[1024];
			int len = 0;
			ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
			while ((len = inputStream.read(bytes)) != -1) {
				outputStream.write(bytes, 0, len);
			}
			
			return outputStream;
		} catch (SocketTimeoutException e) {
			System.out.println("Cannot send request, \n" + url + ", with cookie " + cookie);
			System.out.println(e.toString());
		} catch (Exception e) {
			System.out.println("Cannot send request, \n" + url + ", with cookie " + cookie);
			System.out.println(e.toString());
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (conn != null) {
				conn.disconnect();
			}
		}
		return null;
	}
	
	public String getResult(){
		ByteArrayOutputStream baos;
		String result = "";
		try {
			baos = this.getResponse(url, false, timeoutInMS);
			result = baos.toString("UTF-8");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	public Document getXmlDoc(){
		String content = this.getResult();
		
		int start = content.indexOf("<table");
		int end = content.indexOf("</table>");
		content = content.substring(start, end + 8);
		Document result = null;
		try {
			result = DocumentHelper.parseText(content);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		HttpUtil client = new HttpUtil("http://kaijiang.500wan.com/static/info/kaijiang/xml/ssq/list.xml?_A=MWCVPBKF1340617086163");
//		Document doc = client.getXmlDoc();
		System.out.println(client.getResult());
	}

}
